#!/bin/sh

WORK_DIR=`dirname $0`

# Check arguments
if (( $# < 5 )); then
	echo "Uso: $0 <database> <database_user> <database_password> <path_to_deploy_dir> <config_extension>"
	exit 1
fi


DB_NAME=$1
DB_USER=$2
DB_PASSWORD=$3
LIME_SOURCES_DIR=$WORK_DIR/limesurvey/limesurvey-182
LIME_INSTALL_DIR=$4
CONFIG_EXT=$5


# Deploy
if ! [ -f "$LIME_SOURCES_DIR/config-$CONFIG_EXT.php" ]; then
	cp $LIME_SOURCES_DIR/config.php $LIME_SOURCES_DIR/config-$CONFIG_EXT.php
	echo "Por favor, edite $LIME_SOURCES_DIR/config-$CONFIG_EXT.php convenientemente"
fi
sudo mkdir -p $LIME_INSTALL_DIR
sudo cp -r $LIME_SOURCES_DIR/* $LIME_INSTALL_DIR
sudo cp $LIME_SOURCES_DIR/config-$CONFIG_EXT.php  $LIME_INSTALL_DIR/config.php


# Set permissions
cd $LIME_INSTALL_DIR
sudo chmod -R 775 *
sudo chmod -R 664 *.*
sudo chmod -R 775 tmp
sudo chmod -R 775 templates
sudo chmod -R 775 upload
sudo chmod -R 775 admin
sudo chown -R www-data.www-data $LIME_INSTALL_DIR
for i in `find $LIME_INSTALL_DIR -name .svn`; do sudo rm -Rf $i; done

# Drop database if exists
#echo 'drop database if exists '$DB_NAME';' | mysql -u $DB_USER --password=$DB_PASSWORD mysql
#echo 'create database if not exists '$DB_NAME' default character set utf8;' | mysql -u $DB_USER --password=$DB_PASSWORD mysql

